/*
 * @Author: your name
 * @Date: 2021-08-25 15:39:47
 * @LastEditTime: 2021-09-08 11:17:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bigdate\src\store\module\user.ts
 */

import { State } from '../ntils/hooks'
import { Module } from 'vue'
import { UserDATA } from '../../router/type'
import { SetLocalStrong, SetAesEncryLocal, ClearLocalItem } from '../../ntils/hooks'
import { getOpenKeepAlive } from '../../setting/setRouter'
// 在单纯的ts文件下使用路由跳转需要将路由实例导出来
import { router, resetRouter } from '../../router/index'

export default {
    namespaced: true,
    state: {
        // 用户名称
        userinfo: null,
        // token
        token: null,
    },
    mutations: {
        USER_LOGIN: (state, val) => {
            // 用户登录的所有信息 
            state.userinfo = val 
            // 加密token
            SetLocalStrong({ key: 'roles', value: SetAesEncryLocal(val.roles[0]) })
            // 用户登录之后 处理完路由之后进行
            setTimeout(() => {
                router.push('/')
            }, 100)
            // 用户登录成功后 先过滤路由然后添加进去
        },
        USER_OUTLOGIN: () => {
            ClearLocalItem('roles')
            resetRouter()
        },   
    },
} as Module <UserDATA, State>
